public member function
<memory>
pointer operator->() const noexcept;
Dereference object member
Returns a pointer to the managed object in order to access one of its members.
This member function is exclusive of the non-specialized version of unique_ptr (for single objects). The array specialization does not include it.
Return value
A pointer to the object managed by the unique_ptr.
pointer is a member type, defined as the pointer type that points to the type of object managed.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// unique_ptr::operator->
#include <iostream>
#include <memory>
struct C { int a; int b; };
int main () {
std::unique_ptr<C> foo (new C);
std::unique_ptr<C> bar;
foo->a = 10;
foo->b = 20;
bar = std::move(foo);
if (foo) std::cout << "foo: " << foo->a << ' ' << foo->b << '\n';
if (bar) std::cout << "bar: " << bar->a << ' ' << bar->b << '\n';
return 0;
}
|
Output: